package cn.javaxc.mvcwork.modules.site;
import cn.javaxc.mvcwork.framework.beans.TFormBean;
import cn.javaxc.mvcwork.framework.database.ConnectionElement;
import cn.javaxc.mvcwork.util.common.DatabaseUtils;
import java.sql.*;




public class Site extends TFormBean {

  private int id = -1;
  private String siteCode = null;
  private String virtualHost = null;
  private String databaseUrl = null;
  private String databaseName = null;
  private int databasePort = -1;
  private String databaseUsername = null;
  private String databasePassword = null;
  private String databaseDriver = null;
  private String accessCode = null;
  private boolean enabled = true;
  private String language = null;


  
  public Site() {
  }


  
  public Site(ResultSet rs) throws SQLException {
    this.buildRecord(rs);
  }


  
  public Site(Connection db, int siteId) throws SQLException {
    if (siteId == -1) {
      throw new SQLException("Site record not found.");
    }
    PreparedStatement pst = db.prepareStatement(
        "SELECT * " +
        "FROM ts_sites s " +
        "WHERE site_id = ? ");
    pst.setInt(1, siteId);
    ResultSet rs = pst.executeQuery();
    if (rs.next()) {
      buildRecord(rs);
    }
    rs.close();
    pst.close();
  }


  
  public void setId(int tmp) {
    this.id = tmp;
  }


  
  public void setId(String tmp) {
    this.id = Integer.parseInt(tmp);
  }


  
  public void setSiteCode(String tmp) {
    this.siteCode = tmp;
  }


  
  public void setVirtualHost(String tmp) {
    this.virtualHost = tmp;
  }


  
  public void setDatabaseUrl(String tmp) {
    this.databaseUrl = tmp;
  }


  
  public void setDatabaseName(String tmp) {
    this.databaseName = tmp;
  }


  
  public void setDatabasePort(int tmp) {
    this.databasePort = tmp;
  }


  
  public void setDatabasePort(String tmp) {
    this.databasePort = Integer.parseInt(tmp);
  }


  
  public void setDatabaseUsername(String tmp) {
    this.databaseUsername = tmp;
  }


  
  public void setDatabasePassword(String tmp) {
    this.databasePassword = tmp;
  }


  
  public void setDatabaseDriver(String tmp) {
    this.databaseDriver = tmp;
  }


  
  public void setAccessCode(String tmp) {
    this.accessCode = tmp;
  }


  
  public void setEnabled(boolean tmp) {
    this.enabled = tmp;
  }


  
  public void setEnabled(String tmp) {
    this.enabled = DatabaseUtils.parseBoolean(tmp);
  }


  
  public int getId() {
    return id;
  }


  
  public String getSiteCode() {
    return siteCode;
  }


  
  public String getVirtualHost() {
    return virtualHost;
  }


  
  public String getDatabaseUrl() {
    return databaseUrl;
  }


  
  public String getDatabaseName() {
    return databaseName;
  }


  
  public int getDatabasePort() {
    return databasePort;
  }


  
  public String getDatabaseUsername() {
    return databaseUsername;
  }


  
  public String getDatabasePassword() {
    return databasePassword;
  }


  
  public String getDatabaseDriver() {
    return databaseDriver;
  }


  
  public String getAccessCode() {
    return accessCode;
  }


  
  public boolean getEnabled() {
    return enabled;
  }

  public String getLanguage() {
    return language;
  }

  public void setLanguage(String s) {
    language = s;
  }

  
  public void buildRecord(ResultSet rs) throws SQLException {
    //sites table
    id = rs.getInt("site_id");
    siteCode = rs.getString("sitecode");
    virtualHost = rs.getString("vhost");
    databaseUrl = rs.getString("dbhost");
    databaseName = rs.getString("dbname");
    databasePort = rs.getInt("dbport");
    databaseUsername = rs.getString("dbuser");
    databasePassword = rs.getString("dbpw");
    databaseDriver = rs.getString("driver");
    accessCode = rs.getString("code");
    enabled = rs.getBoolean("enabled");
    language = rs.getString("language");
  }

  public ConnectionElement getConnectionElement() {
    ConnectionElement ce = new ConnectionElement(
        databaseUrl, databaseUsername, databasePassword);
    ce.setDriver(databaseDriver);
    ce.setDbName(databaseName);
    return ce;
  }

}
